package com.qf.java2106.huangshengrui.hotel.controller.front;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.qf.java2106.huangshengrui.hotel.common.ResultVO;
import com.qf.java2106.huangshengrui.hotel.constant.MethodConstant;
import com.qf.java2106.huangshengrui.hotel.controller.BaseServlet;
import com.qf.java2106.huangshengrui.hotel.factory.BeanFactory;
import com.qf.java2106.huangshengrui.hotel.pojos.Food;
import com.qf.java2106.huangshengrui.hotel.service.IFoodService;
import com.qf.java2106.huangshengrui.hotel.service.IFoodTypeService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author q
 * @version V1.0
 * @date 2021/10/27
 **/
@WebServlet("/menu")
public class MenuController extends BaseServlet {

    private IFoodService foodService = (IFoodService) BeanFactory.getBean("foodService");

    private IFoodTypeService foodTypeService = (IFoodTypeService) BeanFactory.getBean("foodTypeService");

    /**
     * 获取所有菜系
     * @param request
     * @param response
     */
    public String getTypes(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultVO vo = foodTypeService.findAllType();
        request.getSession().setAttribute("types",vo.getData());
        return MethodConstant.FORWARD_PREFIX + request.getContextPath() + "/front/";
    }

    /**
     * 去菜系详情
     * @param request
     * @param response
     */
    public String detail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
        ResultVO vo = foodService.findFoodById(foodId);
        if (vo.getSuccess()){
            request.setAttribute("food",vo.getData());
            return MethodConstant.FORWARD_PREFIX + request.getContextPath() +"/front/detail/caixiangxi.jsp";
        } else {
            return (String) vo.getData();
        }
    }


    /**
     * 获取页面菜品
     * @param request
     * @param response
     */
    public String getPageFood(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String pageNumStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        String typeIdStr = request.getParameter("typeId");
        String foodName = request.getParameter("foodName");
        System.out.println("foodName = " + foodName);
        if (StringUtils.isEmpty(pageNumStr)){
            pageNumStr = "1";
        }
        if (StringUtils.isEmpty(pageSizeStr)){
            pageSizeStr = "6";
        }
        if (StringUtils.isEmpty(typeIdStr)){
            typeIdStr = "-1";
        }
        Integer pageNum = Integer.valueOf(pageNumStr);
        Integer pageSize = Integer.valueOf(pageSizeStr);
        Integer typeId = Integer.valueOf(typeIdStr);
        ResultVO vo = foodService.findPageFood(pageNum, pageSize, typeId, foodName);
        if (vo.getSuccess()){
            request.setAttribute("pageFood",vo.getData());
            request.setAttribute("typeId",typeId);
            request.setAttribute("foodName", foodName);
            return MethodConstant.FORWARD_PREFIX + request.getContextPath() + "/front/detail/caidan.jsp";
        } else {
            return (String) vo.getData();
        }

    }



}
